#include "Semaphore_Bulider.hpp"


int main()
{   
    SemaphoreBulider bulider;
    bulider.Set_Num(3).Set_Perm(0600).Set_Value({1,2,3});
    std::shared_ptr<Director> dir = std::make_shared<Director>();
    dir->Construct(bulider , SEM_CRAEY);

    srand((unsigned)time(nullptr));

    std::shared_ptr<Semaphore> fsem = bulider.Get_Sem(); 

    std::cout << std::unitbuf;
    int pid = fork();
    if (pid == 0)
    {
        dir->Construct(bulider , SEM_GET);
        auto csem= bulider.Get_Sem(); 
        for (int i = 0; i < 10; i++)
        {
            csem->P(0);

            std::cout << "A";
            usleep(rand() % 100860);
            std::cout << "A";
            usleep(rand() % 1860);
            csem->V(0);
        }
        exit(0);
    }

    for (int i = 0; i < 10; i++)
    {
        fsem->P(0);

        std::cout << "B";
        usleep(rand() % 100860);
        std::cout << "B";
        usleep(rand() % 1860);
        fsem->V(0);
    }
    waitpid(pid , nullptr , 0);
    return 0;
}